package be.bonamis.umpire.old.utils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import be.bonamis.umpire.old.domain.CsvDispoArbitres;
import be.bonamis.umpire.old.domain.CsvDispoEvent;
import be.bonamis.umpire.old.domain.CsvDispoJournees;
import be.bonamis.umpire.old.domain.XlsCaUmpire;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

public class ImportData {
	public static XlsCaUmpire getXlsCaUmpirebyId(List<XlsCaUmpire> list, final int idToFind) {

		return Iterables.find(list, new Predicate<XlsCaUmpire>() {
			public boolean apply(XlsCaUmpire umpire) {
				return Integer.parseInt(umpire.getOLdId()) == idToFind;
			}
		}, null);
	}

	public static List<CsvDispoArbitres> getOldDbUmpiresList(String filename) {
		ColumnPositionMappingStrategy<CsvDispoArbitres> strat = new ColumnPositionMappingStrategy<CsvDispoArbitres>();
		strat.setType(CsvDispoArbitres.class);
		String[] columns = new String[] { "id", "nomArbitre", "prenomArbitre", "emailArbitre",
				"typeArbitre", "classment", "login", "passwd", "admin" };

		strat.setColumnMapping(columns);

		CsvToBean<CsvDispoArbitres> csv = new CsvToBean<CsvDispoArbitres>();

		CSVReader csvReader = null;
		InputStream in = ImportData.class.getClassLoader().getResourceAsStream(filename);

		csvReader = new CSVReader(new InputStreamReader(in), ';');
		return csv.parse(strat, csvReader);
	}

	public static List<CsvDispoJournees> getOloldDbGameDaysList(String filename) {
		ColumnPositionMappingStrategy<CsvDispoJournees> strat = new ColumnPositionMappingStrategy<CsvDispoJournees>();
		strat.setType(CsvDispoJournees.class);
		String[] columns = new String[] { "idJourn", "dateJourn", "typeChamp", "blocage" };

		strat.setColumnMapping(columns);

		CsvToBean<CsvDispoJournees> csv = new CsvToBean<CsvDispoJournees>();

		CSVReader csvReader = null;
		InputStream in = ImportData.class.getClassLoader().getResourceAsStream(filename);

		csvReader = new CSVReader(new InputStreamReader(in), ';');
		return csv.parse(strat, csvReader);
	}

	public static List<CsvDispoEvent> getOloldDbEventssList(String filename) {
		ColumnPositionMappingStrategy<CsvDispoEvent> strat = new ColumnPositionMappingStrategy<CsvDispoEvent>();
		strat.setType(CsvDispoEvent.class);
		String[] columns = new String[] { "id", "idArbitr", "idJour", "valRep", "remarq" };

		strat.setColumnMapping(columns);

		CsvToBean<CsvDispoEvent> csv = new CsvToBean<CsvDispoEvent>();

		CSVReader csvReader = null;
		InputStream in = ImportData.class.getClassLoader().getResourceAsStream(filename);

		csvReader = new CSVReader(new InputStreamReader(in), ';');
		return csv.parse(strat, csvReader);
	}
}
